package top.hepingan.java.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.hepingan.java.common.Result;
import top.hepingan.java.entity.AUser;

import top.hepingan.java.mapper.AUserMapper;
import top.hepingan.java.service.IUserService;

@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl  extends ServiceImpl<AUserMapper, AUser> implements IUserService {
    @Override
    public int login(String username, String password) {
        LambdaQueryWrapper<  AUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AUser::getUsername,username).eq(AUser::getPassword,password);
        AUser user = this.getOne(lambdaQueryWrapper);
        if (user!=null){
            return user.getId();
        }
        return 0;
    }

    @Override
    public Result registr(AUser user) {
        //用户唯一性判断
        LambdaQueryWrapper<AUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AUser::getUsername,user.getUsername());
        if (this.getOne(lambdaQueryWrapper)==null){
            this.save(user);
            return Result.ok("注册成功");
        }else
        {
            return Result.error("用户已存在");
        }

    }
}
